<template>
  <div class="app-container">

    <u-page title="开票管理">

      <u-filtered>
        <el-form ref="listQuery" :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="订单号" prop="orderNo">
            <el-input v-model="listQuery.orderNo" placeholder="请输入订单号" clearable style="width: 200px" />
          </el-form-item>
          <el-form-item label="卡号" prop="memberId">
            <el-input v-model="listQuery.memberId" placeholder="会员卡号" clearable style="width: 200px" />
          </el-form-item>
          <el-form-item label="发票号码" prop="tradeNo">
            <el-input v-model="listQuery.tradeNo" placeholder="发票号码" clearable style="width: 200px" />
          </el-form-item>
          <el-form-item label="发票类型" prop="invoiceType">
            <el-select v-model="listQuery.invoiceType" placeholder="发票类型" class="status" clearable>
              <el-option label="全部" value="" />
              <el-option label="电子发票" :value="1" />
              <el-option label="专用发票" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="开票状态" prop="status">
            <el-select v-model="listQuery.status" placeholder="开票状态" class="status" clearable>
              <el-option label="全部" value="" />
              <el-option label="待开票" :value="0" />
              <el-option label="开票中" :value="1" />
              <el-option label="开票失败" :value="2" />
              <el-option label="已完成" :value="3" />
              <el-option label="已作废" :value="4" />
            </el-select>
          </el-form-item>
          <el-form-item label="业务订单号" prop="businessOrderNo">
            <el-input v-model="listQuery.businessOrderNo" placeholder="业务订单号" clearable style="width: 200px" />
          </el-form-item>
          <el-form-item label="开票时间">
            <u-date-time-picker v-model="searchDate" value-format="yyyy-MM-dd HH:mm:ss" @change="changeSearchDate" />
          </el-form-item>
          <el-form-item label="操作员" prop="createdBy">
            <el-input v-model="listQuery.createdBy" placeholder="操作员" clearable style="width: 200px" />
          </el-form-item>
          <el-form-item label="人工处理" prop="checked">
            <el-checkbox v-model="listQuery.checked">是</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
          </el-form-item>
          <el-form-item>
            <el-dropdown>
              <el-button type="primary">
                批量操作<i class="el-icon-arrow-down el-icon--right" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item :disabled="selectData.length===0" @click.native="hdlDel">批量删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-form-item>
        </el-form>
      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
        @selection-change="hdlSelectChange"
      >
        <template v-slot:right>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="{ row }">
              <el-button v-if="row.status >= 3 && row.pdfUrl" type="text" @click="handleDownload(row.id)">下载</el-button>
              <el-button v-if="row.status === 2" type="text" @click="hdlRetry(row)">重开</el-button>
              <el-button type="text" @click="hdlEdit(row)">编辑</el-button>
              <el-button v-if="row.status === 3 && row.ticketType === 0" type="text" @click="hdlCancel(row)">作废</el-button>
              <el-button type="text" @click="hdlDelete(row.id, row.orderNo)">删除</el-button>
            </template>
          </el-table-column>
        </template>
      </u-table>

    </u-page>

    <u-dialog :title="textMap[dialogStatus]" :show.sync="dialogFormVisible" @confirm="hdlUpdate()">
      <el-form ref="dialogForm" :rules="rules" :model="dataForm" label-position="top" label-width="80px">

        <el-descriptions :column="1" border style="margin-bottom: 10px">
          <el-descriptions-item label="订单号">{{ dataForm.orderNo }}</el-descriptions-item>

          <el-descriptions-item label="开票时间">{{ dataForm.createTime }}</el-descriptions-item>

          <el-descriptions-item label="开票类型">
            <el-tag v-if="dataForm.invoiceType === 1" type="success">电子发票</el-tag>
            <el-tag v-if="dataForm.invoiceType === 2" type="success">专用发票</el-tag>
          </el-descriptions-item>

          <el-descriptions-item label="发票金额/元">
            <el-tag type="success">{{ dataForm.totalFee }}</el-tag>
          </el-descriptions-item>

          <el-descriptions-item label="会员卡号" :span="2">
            {{ dataForm.memberId }}
          </el-descriptions-item>

          <el-descriptions-item label="完成时间">
            <div v-if="dataForm.successTime">
              <el-tag type="success">{{ dataForm.successTime }}</el-tag>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="开票状态">
            <el-tag v-if="dataForm.status === -1" type="info">已关闭</el-tag>
            <el-tag v-if="dataForm.status === 0" type="warning">待开票</el-tag>
            <el-tag v-if="dataForm.status === 1" type="warning">开票中</el-tag>
            <el-tag v-if="dataForm.status === 2" type="danger">开票失败</el-tag>
            <el-tag v-if="dataForm.status === 3" type="success">已完成</el-tag>
            <el-tag v-if="dataForm.status === 4" type="info">已作废</el-tag>
          </el-descriptions-item>

          <el-descriptions-item label="异常原因" :span="2">{{ dataForm.note }}</el-descriptions-item>
          <el-descriptions-item label="发票备注" :span="2">
            <el-input v-model="dataForm.remark" placeholder="发票备注" clearable />
          </el-descriptions-item>

        </el-descriptions>

        <el-descriptions :column="1" title="发票抬头" border style="margin-top: 20px">

          <el-descriptions-item label="类型">
            <el-radio-group v-model="dataForm.type">
              <el-radio :label="1">个人/非企业单位</el-radio>
              <el-radio :label="2">企业</el-radio>
            </el-radio-group>
          </el-descriptions-item>

          <el-descriptions-item label="邮箱">
            <el-input v-model="dataForm.email" placeholder="邮箱" clearable />
          </el-descriptions-item>

          <el-descriptions-item label="名称">
            <el-input v-model="dataForm.title" placeholder="抬头名称" clearable />
          </el-descriptions-item>

          <el-descriptions-item label="税号">
            <el-input v-model="dataForm.taxNo" placeholder="税号" clearable />
          </el-descriptions-item>

          <el-descriptions-item v-if="dataForm.type === 2" label="注册电话">
            <el-input v-model="dataForm.mobile" placeholder="注册电话" clearable />
          </el-descriptions-item>

          <el-descriptions-item v-if="dataForm.type === 2" label="注册地址">
            <el-input v-model="dataForm.addr" placeholder="注册地址" clearable />
          </el-descriptions-item>

          <el-descriptions-item v-if="dataForm.type === 2" label="开户银行名称">
            <el-input v-model="dataForm.bankName" placeholder="开户银行名称" clearable />
          </el-descriptions-item>

          <el-descriptions-item v-if="dataForm.type === 2" label="开户银行账号">
            <el-input v-model="dataForm.bankNo" placeholder="开户银行账号" clearable />
          </el-descriptions-item>

        </el-descriptions>

      </el-form>
    </u-dialog>

  </div>
</template>
<script>
export default {
  data() {
    return {
      api: this.$u.api.PluginsInvoicing,
      // 表格
      columns: [
        {
          prop: 'ticketType',
          label: '开票类型',
          width: '100',
          render: (h, params) => {
            const { ticketType } = params.row
            let title = '蓝票'
            let theme = 'success'
            if (ticketType === 1) {
              title = '红票'
              theme = 'danger'
            }
            return h('el-tag', { props: { type: theme }}, title)
          }
        },
        {
          prop: 'createTime',
          label: '开票时间',
          width: '150',
          render: (h, params) => {
            const { createTime } = params.row
            if (createTime) {
              return h('el-tag', createTime)
            }
          }
        },
        {
          prop: 'orderNo',
          label: '订单号',
          width: '200',
          render: (h, params) => {
            return h('el-tag', params.row.orderNo)
          }
        },
        {
          prop: 'tradeNo',
          label: '发票号码',
          width: '200',
          render: (h, params) => {
            if (params.row.tradeNo) {
              return h('el-tag', params.row.tradeNo)
            }
          }
        },
        {
          prop: 'memberId',
          label: '卡号',
          width: '166',
          render: (h, params) => {
            if (params.row.memberId) {
              return h('el-tag', params.row.memberId)
            }
          }
        },
        {
          prop: 'invoiceType',
          label: '发票类型',
          width: '100',
          render: (h, params) => {
            const { invoiceType } = params.row
            const title = invoiceType === 1 ? '电子发票' : '专用发票'
            return h('el-tag', { props: { type: 'success' }}, title)
          }
        },
        {
          prop: 'totalFee',
          label: '开票金额',
          render: (h, params) => {
            return h('el-tag', params.row.totalFee)
          }
        },
        {
          prop: 'createMemberId',
          label: '申请卡号',
          width: '166',
          render: (h, params) => {
            if (params.row.createMemberId) {
              return h('el-tag', params.row.createMemberId)
            }
          }
        },
        {
          prop: 'businessOrderNo',
          label: '业务订单',
          render: (h, params) => {
            return h('el-tag', params.row.businessOrderNo)
          }
        },
        {
          prop: 'status',
          label: '发票状态',
          width: '100',
          render: (h, params) => {
            const { status } = params.row
            let title = '待开票'
            let type = 'warning'
            if (status === 1) {
              title = '开票中'
              type = 'warning'
            } else if (status === 2) {
              title = '开票失败'
              type = 'danger'
            } else if (status === 3) {
              title = '已完成'
              type = 'success'
            } else if (status === 4) {
              title = '已作废'
              type = 'info'
            }
            return h('el-tag', { props: { type }}, title)
          }
        },
        {
          prop: 'remark',
          label: '发票备注'
        },
        {
          prop: 'note',
          label: '异常原因'
        },
        {
          prop: 'operator',
          label: '操作员'
        }
      ],

      list: [],
      listLoading: true,
      listOptions: {
        mutiSelect: true, // boolean 是否多选
        stripe: true // boolean 斑马纹
      },
      listQuery: {
        pageNumber: 1,
        pageSize: 10,
        totalCount: 1,
        title: '',
        status: ''
      },

      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑开票',
        create: '新增开票'
      },
      dataForm: {},
      rules: {
        email: [
          { required: true, message: '邮箱必填', trigger: ['blur'] }
        ],
        title: [
          { required: true, message: '抬头名称必填', trigger: ['blur'] }
        ]
      },
      // 删除选中数据
      selectData: [],
      newForm: {
        status: true
      }
    }
  },
  created() {
    this.hdlList()
  },
  methods: {
    // 文件下载事件
    handleDownload(id) {
      this.api.download(id)
    },
    // 重开发票
    hdlRetry(row) {
      const self = this
      // 模态对话框组件
      self.$confirm('订单号：' + row.orderNo + '，是否继续重开？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
        // 处理确认事件
        self.api.retry({ orderNo: row.orderNo }).then(() => {
          // 刷新数据列表
          self.hdlList()
          // 通知信息
          self.$u.msg('操作成功')
        })
      }).catch(() => {
      })
    },
    // 作废发票
    hdlCancel(row) {
      const self = this
      // 模态对话框组件
      self.$confirm('订单号：' + row.orderNo + '，是否继续作废？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
        // 处理确认事件
        self.api.cancel({ orderNo: row.orderNo }).then(() => {
          // 刷新数据列表
          self.hdlList()
          // 通知信息
          self.$u.msg('操作成功')
        })
      }).catch(() => {
      })
    }
  }
}
</script>
